package com.example.springbatchdemo.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.JobStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * @Classname NestedDemo
 * @Description TODO
 * @Date 2020/9/19 13:30
 * @Created by kevin
 */
@Configuration
public class NestedDemo {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private Job childJob1Job;
    @Autowired
    private Job childJob2Job;
    @Autowired
    private JobLauncher jobLauncher;


    @Bean
    public Job parentJob( JobRepository repository, PlatformTransactionManager transactionManager ) {
        return jobBuilderFactory.get("parentJob")
                .start(childJobOne(repository, transactionManager))
                .next(childJobTwo(repository, transactionManager))
                .build();
    }

    /**
     * 将子Job转化为父Job中的step对象
     *
     * @return
     */
    @Bean
    public Step childJobTwo( JobRepository repository, PlatformTransactionManager transactionManager ) {
        return new JobStepBuilder(new StepBuilder("childJob2"))
                //指定转化的子Job对象
                .job(childJob2Job)
                // 指定调用Job对象
                .launcher(jobLauncher)
                // 指定持久化对象
                .repository(repository)
                // 指定事务对象
                .transactionManager(transactionManager)
                .build();

    }

    @Bean
    public Step childJobOne( JobRepository repository, PlatformTransactionManager transactionManager ) {

        return new JobStepBuilder(new StepBuilder("childJob1"))
                //指定转化的子Job对象
                .job(childJob1Job)
                // 指定调用Job对象
                .launcher(jobLauncher)
                // 指定持久化对象
                .repository(repository)
                // 指定事务对象
                .transactionManager(transactionManager)
                .build();
    }

}
